package web;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertTrue;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LoginTest0410 {

    static WebDriver driver;

    @BeforeAll
    public static void setUp(){
        driver=new ChromeDriver();
    }

    // 作业1.ceshiren搜索一个帖子并点进去进行查看
    @Test
    @Order(1)
    public void searchPost() throws InterruptedException {
        driver.get("https://ceshiren.com/");
        driver.findElement(By.id("search-button")).click();
        driver.findElement(By.id("search-term")).sendKeys("微信小程序");
        Thread.sleep(3000);
        driver.findElement(By.cssSelector(".main-results .topic-title")).click();
        Thread.sleep(3000);
        assertTrue(driver.getTitle().contains("微信小程序"));
    }

    // 保存cookie
    @Test
    public void saveCookie() throws InterruptedException {
        driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx?from=myhome_baidu");
        Thread.sleep(10000);
        Set<Cookie> cookies=driver.manage().getCookies();
        ObjectMapper objectMapper=new ObjectMapper(new YAMLFactory());
        try {
            objectMapper.writeValue(new File("cookies.yaml"),cookies);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 作业2.企业微信实现自动登录
    @Test
    @Order(2)
    public void login() throws InterruptedException, IOException {
        driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx?from=myhome_baidu");
        Thread.sleep(10000);
        ObjectMapper objectMapper=new ObjectMapper(new YAMLFactory());
        TypeReference<List<HashMap<String,Object>>> referenceType=new TypeReference<List<HashMap<String,Object>>>(){};
        List<HashMap<String,Object>> cookies=objectMapper.readValue(new File("cookies.yaml"),referenceType);
        cookies.forEach(cookie->{
            driver.manage().addCookie(new Cookie(cookie.get("name").toString(),cookie.get("value").toString()));
        });

        driver.navigate().refresh();
        Thread.sleep(10000);
    }

    // 作业3.在通讯录添加成员
    @Test
    @Order(3)
    public void addMbr() throws InterruptedException {
        // 添加成员
        driver.findElement(By.id("menu_contacts")).click();
        Thread.sleep(3000);
        driver.findElement(By.cssSelector(".js_has_member .qui_btn.ww_btn.js_add_member")).click();
        Thread.sleep(3000);
        driver.findElement(By.id("username")).sendKeys("henry");
        driver.findElement(By.id("memberAdd_acctid")).sendKeys("yx123456");
        driver.findElement(By.id("memberAdd_phone")).sendKeys("15112347890");
        driver.findElement(By.cssSelector(".qui_btn.ww_btn.js_btn_save")).click();
        Thread.sleep(3000);
        assertTrue(driver.findElement(By.cssSelector("#member_list tr td[title=henry]")).isDisplayed());
    }

    @AfterAll
    public static void tearDown(){
        driver.quit();
    }
}
